package pl.com.sthaether.mc.main.toolbar;

import javafx.scene.layout.FlowPane;


public class MCToolHolder extends FlowPane {

//    private ImageView cartogramSettingsButton;
//    private ImageView cartodiagramSettingsButton;


    private MCToolHolder() {
        init();
    }

    public static MCToolHolder getInstance() {
        return MCHolder.INSTANCE;
    }

    private void init() {
//        this.cartogramSettingsButton = new ImageView(new Image(getClass().getClassLoader().getResourceAsStream("pl/com/sthaether/mc/graphics/Icon1.png")));
//        this.cartogramSettingsButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
//            @Override
//            public void handle(MouseEvent event) {
//                final Stage dialog = new Stage();
//                dialog.initModality(Modality.APPLICATION_MODAL);
//                dialog.initOwner(MCApplication.getPrimaryStage());
//                VBox dialogVbox = new VBox(20);
//                //TODO add "auto" button
//                TableView<MCColorRange> table = new TableView<>();
//                table.setEditable(true);
//                table.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
//                table.getSelectionModel().setCellSelectionEnabled(true);
//                TableColumn<MCColorRange, Number> fromColumn = new TableColumn<>("From");
//                fromColumn.setCellValueFactory(cellData -> cellData.getValue().fromProperty());
//                fromColumn.setCellFactory(TextFieldTableCell.<MCColorRange, Number>forTableColumn(new NumberStringConverter()));
//
//                TableColumn<MCColorRange, Number> toColumn = new TableColumn<>("To");
//                toColumn.setCellValueFactory(cellData -> cellData.getValue().toProperty());
//                toColumn.setCellFactory(TextFieldTableCell.<MCColorRange, Number>forTableColumn(new NumberStringConverter()));
//                TableColumn<MCColorRange, Color> colorColumn = new TableColumn<>("Color");
//                colorColumn.setCellValueFactory(new PropertyValueFactory<>("color"));
//                colorColumn.setCellFactory(param -> {
//                    TableCell<MCColorRange, Color> cell =
//                            new TableCell<MCColorRange, Color>() {
//                                @Override
//                                public void updateItem(Color c, boolean empty) {
//                                    if (c != null) {
//                                        final ColorPicker cp = new ColorPicker();
//                                        cp.setValue(c);
//                                        setGraphic(cp);
//                                        cp.setOnAction(t -> {
//                                            getTableView().edit(getTableRow().getIndex(), param);
//                                            ((ObjectProperty<Color>) param.getCellObservableValue(getTableRow().getIndex())).setValue(cp.getValue());
//                                        });
//                                    }
//                                }
//                            };
//                    return cell;
//                });
//                table.getColumns().add(fromColumn);
//                table.getColumns().add(toColumn);
//                table.getColumns().add(colorColumn);
//                ////////////
//
//                table.getItems().addAll(MCCurrentState.getInstance().getCurrentMap().getData().getCartogramColorRange().getAll());
//
//                ///////////
//                Button addButton = new Button("Add");
//                addButton.setOnMouseClicked(e -> {
//                    table.getItems().add(new MCColorRange(0, 0, new Color(0, 0, 0, 1)));
//                });
//                HBox buttonsBox = new HBox();
//                Button okButton = new Button("Confirm");
//                okButton.setOnMouseClicked(e -> {
//                    MCCurrentState.getInstance().getCurrentMap().getData().getCartogramColorRange().setAll(table.getItems());
//                    MCCurrentState.getInstance().getCurrentMap().getData().updateLegend();
//                    MCCurrentState.getInstance().getCurrentMap().getData().autoColorByValues();
//
//
//
//                    System.out.println(table.getItems().size());
//                    dialog.close();
//                });
//                Button cancelButton = new Button("Cancel");
//                cancelButton.setOnMouseClicked(e -> {
//                    System.out.println("Canceled");
//                    dialog.close();
//                });
//                buttonsBox.getChildren().addAll(okButton, cancelButton);
//
//                dialogVbox.getChildren().addAll(table, addButton, buttonsBox);
//                Scene dialogScene = new Scene(dialogVbox, 300, 500);
//                dialog.setScene(dialogScene);
//                dialog.show();
//            }
//        });
//        this.cartodiagramSettingsButton = new ImageView(new Image(getClass().getClassLoader().getResourceAsStream("pl/com/sthaether/mc/graphics/Icon2.png")));
//        this.cartodiagramSettingsButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
//            @Override
//            public void handle(MouseEvent event) {
//                final Stage dialog = new Stage();
//                dialog.initModality(Modality.APPLICATION_MODAL);
//                dialog.initOwner(MCApplication.getPrimaryStage());
//                VBox dialogVbox = new VBox(20);
//                //TODO add "auto" button
//                TableView<MCSeriesColor> table = new TableView<>();
//                table.setEditable(true);
//                table.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
//                table.getSelectionModel().setCellSelectionEnabled(true);
//                TableColumn<MCSeriesColor, String> seriesColumn = new TableColumn<>("Series");
//                seriesColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
//                seriesColumn.setCellFactory(TextFieldTableCell.forTableColumn());
//                seriesColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<MCSeriesColor, String>>() {
//                    @Override
//                    public void handle(TableColumn.CellEditEvent<MCSeriesColor, String> event) {
//                        MCSeriesColor value = event.getRowValue();
//                        String val = event.getNewValue();
//                        if (val != null && !val.equals("")) {
//                            if (!val.equals("Add")) {
//
//                                table.getItems().add(new MCSeriesColor("Add", new Color(0, 0, 0, 1)));
//                                value.nameProperty().setValue(val);
//
//                            }
//                        } else if (table.getItems().size() > 1 && !event.getOldValue().equals("Add")) {
//                            table.getItems().remove(value);
//                        }
////                        table.layout();
//                    }
//
//                });
////                new EventHandler<TreeTableColumn.CellEditEvent<MCFilterableTreeTableNode, String>>() {
////                    @Override
////                    public void handle(TreeTableColumn.CellEditEvent<MCFilterableTreeTableNode, String> event) {
////                        final TreeItem<MCFilterableTreeTableNode> selectedItem = table.getSelectionModel().getSelectedItem();
////                        String name = event.getNewValue();//table.getSelectionModel().getSelectedItem().getValue().nameProperty().getValue();
////                        if (name != null && !name.equals("")) {
////                            if (!name.equals("Add")) {
////                                final TreeItem<MCFilterableTreeTableNode> newItem =
////                                        new TreeItem<>(new MCFilterableTreeTableNode(new SimpleStringProperty("Add"), null));
////                                selectedItem.getChildren().add(newItem);
////
////                                final int rowIndex = table.Row(newItem);
////                                table.getSelectionModel().getSelectedItem().getValue().nameProperty().setValue(name);
//////                        table.edit(rowIndex, columnName);//Does 99% not start or get canceled, why?
////                            }
////                        } else if (selectedItem.getChildren().size() > 1 && !event.getOldValue().equals("Add")) {
////                            selectedItem.getChildren().remove(table.getSelectionModel().getSelectedItem());
////                        } else {
////                            table.getSelectionModel().getSelectedItem().getValue().nameProperty().setValue("Add");
////                        }
////
////                    }
////                }
//
//                TableColumn<MCSeriesColor, Color> colorColumn = new TableColumn<>("Color");
//                colorColumn.setCellValueFactory(new PropertyValueFactory<>("Color"));
//                colorColumn.setCellFactory(param -> {
//                    TableCell<MCSeriesColor, Color> cell =
//                            new TableCell<MCSeriesColor, Color>() {
//                                @Override
//                                public void updateItem(Color c, boolean empty) {
//                                    if (c != null && !empty) {
//                                        final ColorPicker cp = new ColorPicker();
//                                        cp.setValue(c);
//                                        setGraphic(cp);
//                                        cp.setOnAction(t -> {
////                                            getTableView().edit(getTableRow().getIndex(), param);
//                                            ((ObjectProperty<Color>) param.getCellObservableValue(getTableRow().getIndex())).setValue(cp.getValue());
//                                        });
//                                    }
//                                }
//                            };
//                    return cell;
//                });
//                table.getColumns().add(seriesColumn);
//                table.getColumns().add(colorColumn);
//                ////////////
////                MCCurrentState.getInstance().getCurrentMap().getData().getCartogramColorRange().
//
//                if (table.getItems().size() < 1) {
//                    table.getItems().add(new MCSeriesColor("Add", new Color(0, 0, 0, 1)));
//                }
//                ///////////
////                dialogVbox.getChildren().add(table);
//                HBox buttonsBox = new HBox();
//                Button okButton = new Button("Confirm");
//                okButton.setOnMouseClicked(e -> {
//                    System.out.println(table.getItems().size());
//                    MCCurrentState.getInstance().getCurrentMap().getData().updateLegend();
//
//                    dialog.close();
//                });
//                Button cancelButton = new Button("Cancel");
//                cancelButton.setOnMouseClicked(e -> {
//                    System.out.println("Canceled");
//                    dialog.close();
//                });
//                buttonsBox.getChildren().addAll(okButton, cancelButton);
//
//                dialogVbox.getChildren().addAll(table, buttonsBox);
//                Scene dialogScene = new Scene(dialogVbox, 300, 500);
//                dialog.setScene(dialogScene);
//                dialog.show();
//            }
//        });
//        this.getChildren().addAll(this.cartogramSettingsButton, this.cartodiagramSettingsButton);
    }


    private static class MCHolder {
        private static final MCToolHolder INSTANCE = new MCToolHolder();
    }
}
